package Annotation.DataBase;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * had been test on 2013.05.25 - used success!
 * @author user
 *
 */
public class MySQLConnection {
	private Connection conn = null; //Database objects 
	private Statement st = null;
	private ResultSet rs = null;
	
	private String url = "jdbc:mysql://localhost:3306/annotationclusterdb?autoReconnect=true";
	private String user = "root";
	private String password = "addmin";
	
	protected void BuildConnection(){

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("MySQL Database: Where is your MySQL JDBC Driver?");
			e.printStackTrace();
			return;
		}
		System.out.println("MySQL Database: MySQL JDBC Driver Registered!");

		try {
			conn = DriverManager.getConnection(url, user, password);
	 
		} catch (SQLException e) {
			System.out.println("MySQL Database: Connection Failed! Check output console");
			e.printStackTrace();
			return;
		}
		
		if (conn != null) {
			System.out.println("MySQL Database: You made it, take control your database now!");
		} else {
			System.out.println("MySQL Database: Failed to make connection!");
		}
		
	}
	
	protected ResultSet selectSQL(String SQL){
		
		//SELECT school FROM school
		try{
			//System.out.println("MySQL_ connection testing");
		
	        st = conn.createStatement();
	        
	        //rs = st.executeQuery("SELECT school FROM school;");
	        rs = st.executeQuery(SQL);
        
		}catch (SQLException e) {
			//System.out.println("");
		}
		return rs;
	}
	
	//protected void actionSQL(String SQL){
	protected void commond(String SQL){

		try{
			//System.out.println("MySQL_ connection testing");
		
	        st = conn.createStatement();
	        st.executeUpdate(SQL);
			
		}catch (SQLException e) {
			//System.out.println("");
		}
		
	}
	
	protected void closeConnection(){
		try {
		    if (rs != null) {
		        rs.close();
		    }
		    if (st != null) {
		        st.close();
		    }
		    if (conn != null) {
		        conn.close();
		    }
		    System.out.println("MySQL Database: DataBase connection closed");
		} catch (SQLException e) {
		    System.out.println("MySQL Database: Close Exception :" + e.toString());
		}
	}
	
	
}
